// 我们约定top指向栈顶元素所在位置

function Node(val = 0, next = null) {
    this.val = val
    this.next = next
}

function Stack() {
    this.top = null
}

// 初始化空栈
Stack.prototype.init = function () {
    this.top = null
}

// 判断栈是否为空
Stack.prototype.isEmpty = function () {
    this.top === null
}

// 入栈操作
Stack.prototype.push = function (value) {
    let cur = new Node(value)
    cur.next = this.top
    this.top = cur
}

// 出栈操作
Stack.prototype.pop = function () {
    if (this.isEmpty()) {
        throw new Error('Stack is Empty')
    } else {
        this.top = this.top.next
    }
}

// 获取栈顶元素
Stack.prototype.peek = function () {
    if (this.isEmpty()) {
        throw new Error('Stack is Empty')
    } else {
        return this.top.value
    }
}